import { baseUrlApi, requestClient, type Result } from '#/api/request';

/** 获取用户管理列表 */
export const getUserList = (data?: object) => {
  return requestClient.post<Result>('/user', data);
};

/** 获取系统配置 */
export const getOption = (data: { option: string }) => {
  return requestClient.post<Result>(
    baseUrlApi('/admin/system/getOption'),
    data,
  );
};

/** 写入系统配置 */
export const putOption = (data: { data: any; key: string }) => {
  return requestClient.put<Result>(baseUrlApi('/admin/system/setOption'), data);
};

/** 获取角色管理列表 */
export const getRoleList = (data?: object) => {
  return requestClient.post<Result>('/role', data);
};

/** 获取部门管理列表 */
export const getDeptList = (data?: object) => {
  return requestClient.post<Result>('/dept', data);
};

/** 资源列表 */
export const getAssets = (params?: object) => {
  return requestClient.get<Result>(baseUrlApi('/admin/assets'), { params });
};

/** 删除资源 */
export const deleteAsset = (data?: any) => {
  return requestClient.delete<Result>(baseUrlApi(`/admin/assets/${data.id}`));
};
export const folder = (data?: any) => {
  return requestClient.get<Result>(baseUrlApi(`/admin/assetsfolder`));
};

export const toWebp = (params?: object) => {
  return requestClient.get<Result>(baseUrlApi('/admin/toWebp'), { params });
};

/** 回收站文件列表 */
export const getRecycleBinItems = (params?: object) => {
  return requestClient.get<Result>(baseUrlApi('/admin/recycle/bin/items'), {
    params,
  });
};

/** 彻底删除回收站文件 */
export const deleteRecycleBinItems = (data?: object) => {
  return requestClient.delete<Result>(
    baseUrlApi('/admin/recycle/bin/items'),
    data,
  );
};

/** 回收站文件还原 */
export const postRecycleBinRestore = (data?: object) => {
  return requestClient.post<Result>(
    baseUrlApi('/admin/recycle/bin/restore'),
    data,
  );
};

/** 清空回收站 */
export const deleteRecycleBinClear = () => {
  return requestClient.delete<Result>(baseUrlApi('/admin/recycle/bin/clear'));
};

/** 文件上传 */
export const postUpload = (option: any) => {
  const formData: any = new FormData();
  const filename = option.filename || 'file';
  formData.append(filename, option.file, option.file.name);
  if (option.data) {
    for (const [key, value] of Object.entries(option.data)) {
      if (Array.isArray(value) && value.length > 0)
        formData.append(key, ...value);
      else formData.append(key, value);
    }
  }

  const headers = Object.assign(
    {},
    { 'Content-Type': 'multipart/form-data;' },
    option.headers,
  ) || { 'Content-Type': 'multipart/form-data;' };

  const method = option.method || 'post';

  return requestClient.post<Result>(baseUrlApi('/user/upload/one'), formData, {
    method,
    headers,
  });
};

/** 获取系统信息 */
export const getSystemInfo = () => {
  return requestClient.get<Result>(
    baseUrlApi('/plugin/system_info/api_index/index'),
  );
};

/** 导入API */
export const postSwaggerImportApi = () => {
  return requestClient.post<Result>(
    baseUrlApi('/plugin/swagger/api_admin_index/importApi'),
  );
};

/** 获取操作日志 */
export const getSystemLogs = (params?: object) => {
  return requestClient.get<Result>(baseUrlApi('/admin/system/index'), {
    params,
  });
};
